<template>
  <div class="el-card__body">
    <!-- 头部搜索，新增 -->
    <div class="subject-bar">
      <div class="left-form">
        <el-form
          ref="formName"
          :inline="true"
          :model="formName"
          class="demo-form-inline"
        >
          <el-form-item label="标签名称">
            <el-input v-model.trim="formName.tagName" />
          </el-form-item>
          <el-form-item label="状态">
            <el-select
              v-model="formName.state"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="resetForm">清除</el-button>

            <el-button type="primary" @click="searchTags">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="right-form">
        <el-button type="success" @click="addTags">
          <i class="el-icon-edit" />
          新增标签</el-button>
      </div>
    </div>
    <!-- 表格 -->
    <el-table v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="序号" type="index" />
      <el-table-column prop="subjectName" label="所属学科" />
      <el-table-column prop="tagName" label="标签名称" />
      <el-table-column prop="username" label="创建者" />
      <el-table-column prop="addDate" label="创建日期">
        <template slot-scope="{ row }">{{ row.addDate |formatDate }}</template>
      </el-table-column>
      <el-table-column prop="state" label="状态">
        <template v-slot="{ row }">
          {{ row.state ? "已启用" : "已禁用" }}</template></el-table-column>

      <el-table-column label="操作" width="200">
        <template v-slot="{ row }">
          <el-button type="text" @click="changeState(row)">{{
            row.state ? "禁用" : "启用"
          }}</el-button>
          <el-button
            type="text"
            :disabled="row.state ? true : false"
            @click="editTags(row.id)"
          >修改</el-button>
          <el-button
            type="text"
            :disabled="row.state ? true : false"
            @click="delTags(row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页模块 -->
    <el-row type="flex" justify="end" class="pagerow">
      <el-pagination
        background
        layout="prev, pager, next,sizes,jumper"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="page.pagesize"
        :current-page="page.page"
        :total="page.total"
        @current-change="changePage"
        @size-change="changeSize"
      />
    </el-row>
    <!-- 新增编辑目录弹出层 -->
    <dialog-tags ref="dialogTags" @updateTags="getTags" />
  </div>
</template>
<script>
import { getTags, changeState, delTags, getTagsById } from '@/api/subjects/tags'
import DialogTags from './components/dialogTags.vue'
const defaultItem = {
  page: 1,
  pagesize: 5,
  total: 0
}
export default {
  name: 'Tags',
  components: { DialogTags },
  props: {},
  data() {
    return {
      page: {
        ...defaultItem
      },
      loading: false,
      formName: {
        tagName: '',
        state: ''
      },
      options: [
        {
          value: 0,
          label: '禁用'
        },
        {
          value: 1,
          label: '启用'
        }
      ],
      tableData: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getTags()
    this.getTagsById()
  },
  mounted() {},
  methods: {
    // 获取目录列表
    async getTags() {
      this.loading = true
      const { data } = await getTags({ ...this.page })
      this.tableData = data.items
      this.page.total = data.counts
      this.loading = false
    },

    // 页码，每页条数改变时，判断筛选条件
    async selectTags() {
      if (this.formName.state === 0 || this.formName.state === 1) {
        const { data } = await getTags({
          ...this.page,
          // ...defaultItem,
          tagName: this.formName.tagName,
          state: this.formName.state === 0 ? 0 : 1
        })
        this.tableData = data.items
        this.page.total = data.counts
      } else {
        console.log(this.formName.tagName)
        const { data } = await getTags({
          ...this.page,
          tagName: this.formName.tagName
        })
        this.tableData = data.items
        this.page.total = data.counts
      }
    },
    // 页码改变时，传最新页码值
    changePage(newPage) {
      this.page.page = newPage
      // 判断筛选条件
      this.selectTags()
    },
    // 每页条数改变时
    async changeSize(newSize) {
      this.page.pagesize = newSize
      this.page.page = 1
      // 判断筛选条件
      this.selectTags()
    },

    // 搜索，传初始page项
    async searchTags() {
      this.page = { ...defaultItem }
      // state状态为0时，为1时；没有state状态时，不传state;bug的解决双等，全等0？？？
      this.selectTags()
      // 页码设为1，（解决在第n页搜索时的bug）
      this.page.page = 1
    },
    // 表单清除，重置
    resetForm() {
      this.formName = {
        tags: '',
        state: ''
      }
      this.getTags()
    },
    // 修改标签
    async editTags(id) {
      await this.$refs.dialogTags.getTagsDetail(id)
      this.$refs.dialogTags.showdialogTags = true
    },
    // 新增标签
    addTags() {
      this.$refs.dialogTags.showdialogTags = true
    },
    // 删除标签
    async delTags(id) {
      try {
        await this.$confirm('确认删除该标签吗？')
        await delTags(id)
        if (this.tableData.length === 1 && this.page.page !== 1) {
          this.page.page--
        }
        this.$message.success('删除成功')
        this.getTags()
      } catch (error) {
        console.log(error)
      }
    },
    // 改变状态
    async changeState(row) {
      row.state = row.state ? 0 : 1
      await changeState(row)
      // this.getTags();
      this.$message.success('修改成功')
    },
    //  页面跳转，根据id展示数据详情
    async getTagsById() {
      const subjectID = this.$route.query.id
      console.log(subjectID)
      const { data } = await getTagsById({
        ...defaultItem,
        subjectID
      })
      this.tableData = data.items
      this.page.total = data.counts
    }
  }
}
</script>
<style scoped lang="scss">
.subject-bar {
  display: flex;
  justify-content: space-between;
}
.pagerow{
  margin: 15px 0;
}
</style>
